<?php
namespace App\Models;

use App\Models\Scope\NeighborCouncilComplaintsScope;
use Illuminate\Support\Facades\DB;

class NeighborCouncilComplaints extends CommonModel {
	protected $table = 'neighbor_council_complaints';
	
//	public static function boot()
//	{
//		parent::boot();
//		static::addGlobalScope(new NeighborCouncilComplaintsScope());
//	}
	
	/**
	 * @return $this
	 * 信息详情，使用：NeighborCouncilComplaints::findOrFail($id)->detail()
	 */
	public function detail(){
		$id = $this->getAttribute('id');
		$history = NeighborCouncilComplaints::treeChildrenOne($id);
		$this->setAttribute('admin_realname','');
		$this->setAttribute('history',$history);
		return $this;
	}
	
	/**
	 * @param $parent_id
	 * @return \Illuminate\Support\Collection
	 * 树状查询，单个集合
	 */
	public static function treeChildrenOne($parent_id){
		$return = collect([
			static::select(DB::raw('neighbor_council_complaints.*,u.realname as admin_realname'))->leftJoin('client_user as u','u.id','=','neighbor_council_complaints.admin_uid')->where(['neighbor_council_complaints.id'=>$parent_id])->first(),
		]);
		static::select(DB::raw('neighbor_council_complaints.*,u.realname as admin_realname'))->leftJoin('client_user as u','u.id','=','neighbor_council_complaints.admin_uid')->where(['parent_id'=>$parent_id])->oldest()->get()->each(function($v,$k) use (&$return){
			$parent = static::treeChildrenOne($v->id);
//			$return->push($v);
			$return = $return->merge($parent);
		});
		return $return;
	}
}